const api = require("../../../utils/api");
const layer = require("../../../utils/layer");
const listTouchBehavior = require("../../../utils/listTouchBehavior");
Page({
    // 混入列表左移
    behaviors: [listTouchBehavior],
    /**
     * 页面的初始数据
     */
    data: {
        dataList: [],
        type: 0, // 0 发送者  1 接收者
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.getDataList();
    },
    /**
     * 顶部 nav 选择
     */
    tabSelect(e) {
        this.setData({
            type: e.currentTarget.dataset.value
        })
    },
    /**
     * 获取当前登录用户的消息列表
     */
    getDataList() {
        api.get("/team/getTeamMessages").then(res => {
            this.setData({
                dataList: res.data
            })
        });
    },
    /**
    * 显示对应model
    */
    showModel(e) {
        this.setData({
            modalName: e.currentTarget.dataset.model
        })
    },
    /**
     * 删除对应消息
     */
    delete(e) {
        let id = e.currentTarget.dataset.id
        let that = this;
        layer.showModal(() => {
            layer.showLoading();
            api.get("/team/delete/message", { id }).then(res => {
                layer.hideLoading();
                layer.showToast(res.message);
                setTimeout(() => {
                    that.getDataList();
                }, 300)
            });
        }, "提示", "确定删除这条消息？")
    },
    /**
     * 显示详情内容
     */
    detail(e) {
        let id = e.currentTarget.dataset.id;
        let dataList = this.data.dataList;
        let that = this;
        let message;
        if (dataList && dataList.length > 0) {
            for (let item of dataList) {
                if (item.id == id) {
                    message = item;
                    break;
                }
            }
        }
        if (message) {
            if (message.status != 0) {
                wx.showModal({
                    title: '提示',
                    content: `【${message.user.nickName}】邀请你加入【${message.team.name}】\n 你已${message.status == 1 ? '接受' : '拒绝'}`,
                    showCancel: false,
                    success(res) {
                    }
                })
                return;
            }
            if (message.type == 0) {
                wx.showModal({
                    title: '提示',
                    content: `你邀请【${message.user.nickName}】加入【${message.team.name}】`,
                    showCancel: false,
                    success(res) {
                    }
                })
            } else {
                wx.showModal({
                    title: '提示',
                    content: `【${message.user.nickName}】邀请你加入【${message.team.name}】`,
                    cancelText: "拒绝",
                    confirmText: "接受",
                    success(res) {
                        layer.showLoading();
                        let status = 1; // 接受
                        if (res.cancel) { // 如果点击了拒绝
                            status = 2;
                        }
                        api.get("/team/handle/message", { id, status }).then(res => {
                            layer.hideLoading();
                            layer.showToast(res.message)
                            // 重新拉取数据
                            setTimeout(() => {
                                that.getDataList();
                            })
                        });
                    }
                })
            }
        }
    },
    /**
    * 隐藏model
    */
    hideModal() {
        this.setData({
            modalName: ''
        })
    },
})